package rpcbiz

import (
	"fmt"
	"cloud_worm/wormweb/controllers/ctrinner/analyzer"
	"cloud_worm/util/umodels/mysqldb/dbtask"
	"github.com/astaxie/beego/logs"
	"errors"
	"cloud_worm/util/proto/urpc"
	"cloud_worm/util/unet/rpc"
)


type WpEventPostArgs urpc.WpEventPostArgs

// WeaponEvent.EventPost weapon执行的事件上报
func (w *WeaponEvent) EventPost(args *WpEventPostArgs, reply *urpc.WpEventPostReply) error {
	// 准备分析任务
	wp := &dbtask.Weapon{
		Id: int64(args.WeaponId),
	}
	err := wp.Read("id")
	if err != nil {
		logs.Error(fmt.Sprintf("read weapon record for '%d' error: %s", args.WeaponId, err.Error()))
		return errors.New("read weapon record error")
	}

	// 基于不同策略进行处理
	if wp.Type == dbtask.WeaponTypeNoAnalyzer {
		// do nothing
	} else if wp.Type == dbtask.WeaponTypeCareResult {
		wpErrInfo := &analyzer.WeaponErrorInfo{
			Name: wp.Name,
			WeaponId: int64(args.WeaponId),
			WormId: args.AgentId,
			Ip: args.AgentIP,
			TaskId: args.TaskId,
		}
		analyzer.ComputeQueueInstance().MsgQ.Push(wpErrInfo)
	} else {
		analyzerItem := &analyzer.ComputeItem{
			WormId: args.AgentId,
			Ip: args.AgentIP,
			Name: wp.Name,
			WeaponId: int64(args.WeaponId),
			TaskId: args.TaskId,
		}
		analyzer.ComputeQueueInstance().MsgQ.Push(analyzerItem)
		logs.Debug(fmt.Sprintf("WeaponEventController: %s, %s, %s", analyzerItem.Ip, analyzerItem.Name, args.TaskId))
	}

	return nil
}

type WeaponEvent int

func init()  {
	rpc.Register(new(WeaponEvent))
}